package com.yjj.demo.starter.service;

import com.yjj.demo.common.exception.NoStackException;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 *
 *
 * @author yangjiajia
 * @createdAt 2023/1/19 11:19
 */
@Service
@Log4j2
public class TestService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    public boolean getLock(String key) {
        if (!key.startsWith("test")) {
            throw new NoStackException("redis key设置有误");
        }
        return Boolean.TRUE.equals(redisTemplate.opsForValue().setIfPresent(key, "1", 60, TimeUnit.SECONDS));
    }
}
